cmake_minimum_required(VERSION 3.6)

project(CcTools)

SET(CMAKE_PREFIX_PATH $ENV{QTDIR})

# 找到Qt相关库并导入项目，确保其被正确安装在系统中
find_package(Qt5Core $ENV{QTVERSION} REQUIRED)
find_package(Qt5Gui $ENV{QTVERSION} REQUIRED)
find_package(Qt5Widgets $ENV{QTVERSION} REQUIRED)
find_package(Qt5Network $ENV{QTVERSION} REQUIRED)

file(GLOB_RECURSE All_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.c 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.cc
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.mm 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.hh 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
							  
cc_source_group_by_dir(All_SOURCES)	
					
set(CMAKE_AUTOMOC ON)                    
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Core_EXECUTABLE_COMPILE_FLAGS}")

include_directories(${CMAKE_CURRENT_SOURCE_DIR} 
					${Qt5Core_INCLUDE_DIRS}
					${CMAKE_CURRENT_SOURCE_DIR}/include)

if(WIN32)
	set(exe_property WIN32)
	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT /Zi /MP /Qpar /Gm-")
	set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
	set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")

	add_definitions(-DUNICODE -D_UNICODE -D_SCL_SECURE_NO_WARNINGS -DWIN32 -DCCTOOLS_LIB -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB)
	
elseif(APPLE)
    SET(BUILD_WITH_INSTALL_RPATH TRUE)
    set(exe_property MACOSX_BUNDLE)
    set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")	
	add_definitions(-DQT_CORE_LIB -DQT_GUI_LIB)	
elseif(LINUX)
endif(WIN32)


set(NAME CcTools)

# 生成动态库CcEngine.dylib
add_library(${NAME} SHARED ${All_SOURCES})

if(WIN32)
	add_custom_command(TARGET ${NAME} PRE_BUILD COMMAND if exist ${CMAKE_CURRENT_BINARY_DIR}/$(Configuration) cmake -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/$(Configuration))	
elseif(APPLE)
	set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT "dwarf-with-dsym")
	set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_GCC_GENERATE_DEBUGGING_SYMBOLS "YES")
	set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_SEPARATE_STRIP "YES")
	set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_STRIP_STYLE "Non-Global Symbols")

    target_link_libraries(${NAME} "-framework CoreVideo" "-framework CoreImage" "-framework CoreGraphics" "-framework AVFoundation")

endif()

if(APPLE)
	set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_ARCHS "x86_64")
endif()

# 将Qt的相关模块链接到相关目标（target）中，以使得目标能够使用Qt的相关模块功能
qt5_use_modules(${NAME} Core)
qt5_use_modules(${NAME} Gui)
qt5_use_modules(${NAME} Widgets)
qt5_use_modules(${NAME} Network)
							
target_link_libraries(${NAME} ${Qt5Core_LIBRARIES} ${LINK_LIBRARY})
